package cn.js.sangu.unitStore.controller;

import cn.hutool.core.util.StrUtil;
import cn.js.sangu.unitStore.common.LogType;
import cn.js.sangu.unitStore.common.MyAuthAccess;
import cn.js.sangu.unitStore.common.MyLogsAOP;
import cn.js.sangu.unitStore.common.R;
import cn.js.sangu.unitStore.dto.WebsiteInfoDTO;
import cn.js.sangu.unitStore.entity.*;
import cn.js.sangu.unitStore.service.UserService;
import cn.js.sangu.unitStore.service.WebsiteService;
import cn.js.sangu.unitStore.utils.IpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 说明：
 * 作者：linshuo
 * 日期：2024/8/11 12:45
 */
@CrossOrigin
@RestController
@RequestMapping("website")
public class WebsiteController {

    static final String OPERATION_WEBSITE = "官网模块";

    @Value("${system.ip:localhost}")
    String ip;

//    @Value("${server.port}")
//    String port;

    @Autowired
    WebsiteService websiteService;

    @Autowired
    UserService userService;

    @MyAuthAccess
    @MyLogsAOP(operation = OPERATION_WEBSITE,operaType = LogType.ADD)
    @PostMapping("/info")
    public R submitInfo(@RequestBody WebsiteInfoDTO dto,HttpServletRequest request) {
        if (StrUtil.isBlank(dto.getIpAddr())){
            dto.setIpAddr(IpUtil.getClientIp(request));
        }
        websiteService.saveInfo(dto);
        return R.ok();
    }

    @MyLogsAOP(operation = OPERATION_WEBSITE,operaType = LogType.ADD)
    @RequestMapping("save")
    public R save(@RequestBody WebsiteInfoDTO dto, HttpServletRequest request){
        if (StrUtil.isBlank(dto.getIpAddr())){
            dto.setIpAddr(IpUtil.getClientIp(request));
        }
        websiteService.saveInfo(dto);
        return R.ok();
    }

    @MyLogsAOP(operation = OPERATION_WEBSITE,operaType = LogType.LOOKUP)
    @GetMapping("findByPage")
    public R findByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String keyword){
        QueryWrapper<Website> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(keyword),"company_name",keyword);
        wrapper.orderByDesc("id");

        //构建page对象
        Page<Website> param = new Page<>(pageNum, pageSize);
        Page<Website> res = websiteService.page(param, wrapper);
        return R.ok(res.setRecords(res.getRecords()));
    }

    @MyLogsAOP(operation = OPERATION_WEBSITE,operaType = LogType.UPDATE)
    @PutMapping("update")
    public R update(@RequestBody Website dto){
        return R.ok(websiteService.updateInfo(dto));
    }















//    @MyAuthAccess
//    @PostMapping("upload")
//    public R upload(MultipartFile file) throws IOException {
//        return R.ok(fileService.upload(file));
//    }
//
//    @MyAuthAccess
//    @GetMapping("download/{id}/{today}/{fileName}")
//    public void download(@PathVariable Integer id,
//                         @PathVariable String today,
//                         @PathVariable String fileName,
//                         HttpServletResponse response) throws IOException {
//        //文件默认预览，可改为附件下载方式
//        response.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
//        //预览方式
////        response.addHeader("Content-Disposition","inLine;filename=" + URLEncoder.encode(fileName,"UTF-8"));
//
//        String fullRootPath = fileService.download(0,today,fileName);
//        if (!FileUtil.exist(fullRootPath)) return;
//
//        ServletOutputStream outputStream = response.getOutputStream();
//        outputStream.write(FileUtil.readBytes(fullRootPath));   //文件的字节流数组
//        outputStream.flush(); //刷出去
//        outputStream.close(); //关闭文件流
//    }
//
//    @PostMapping("editor/upload")
//    public Dict editorUpload(@RequestParam MultipartFile file,@RequestParam String type) throws IOException {
//        Object data = null;
//        if ("img".equals(type)){
//            data = CollUtil.newArrayList(Dict.create().set("url", this.upload(file).getData()));
//        }else if ("video".equals(type)){
//            data = Dict.create().set("url",this.upload(file).getData());
//        }
//        return Dict.create().set("errno",0).set("data", data);
//    }
//
//    @MyLogsAOP(operation = OPERATION_FILE,operaType = LogType.LOOKUP)
//    @GetMapping("findByPage")
//    public R findByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String fileName){
//        QueryWrapper<FileNas> wrapper = new QueryWrapper<>();
//        wrapper.like(StrUtil.isNotBlank(fileName),"file_name",fileName);
//        wrapper.orderByDesc("id");
//
//        //构建page对象
//        Page<FileNas> param = new Page<>(pageNum, pageSize);
//        Page<FileNas> res = fileService.page(param, wrapper);
//        return R.ok(res.setRecords(this.suppleName(res.getRecords())));
//    }
//
//    /**
//     * 补充作者和修改人名称
//     */
//    private List<FileNas> suppleName(List<FileNas> list ){
//        for (FileNas item : list) {
//            //作者
//            User author = userService.getById(item.getUserId());
//            if (null != author) item.setUsername(author.getName());
//            //修改人
//            User modifier = userService.getById(item.getModifier());
//            if (null != modifier) item.setModifierName(modifier.getName());
//        }
//        return list;
//    }


}
